#!/bin/bash

before="[Doki-Chihiro] Mashiroiro Symphony - 03 (1280x720 Hi10P BD FLAC) [1002A9F3].mkv"
after="[Doki-Chihiro] Mashiroiro Symphony - 03v2 (1280x720 Hi10P BD FLAC) [E1C28040].mkv"
app="xdelta"

function check_wine {
  hash wine 2>&- && {
    app="wine xdelta3.exe"
  } || {
    echo $1;
    echo "Please install either wine or xdelta3.";
    exit 1;
  }
}

hash xdelta3 2>&- && {
  app="xdelta3";
} || {
  hash xdelta 2>&- && {
    $app -D > /dev/null 2> /dev/null;
    if [ $? -eq 2 ]; then
      check_wine "Wine is not installed and the xdelta installed is of an older version.";
    fi
  } || {
    check_wine "Neither wine or xdelta is installed.";
  }
}

output=`$app -d -s "$before" "patch.delta" "$after"`
if [ $? -ne 0 ];
  then
    echo $output;
    echo "Press enter to exit.";
  else
    echo "Done, your file was patched successfully. Press enter to exit.";
fi
read
exit 0;
